package iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.fragment.producerFragment;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.R;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.adapter.ProductAdapter;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.entity.Product;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service.IProductService;

public class FragmentProductManagement extends Fragment {

    private ListView productListView;
    private Button previousPage, nextPage;
    private TextView currentPage;
    private Context fragmentContext;
    private EditText searchInput;
    private Button searchButton;

    private List<Product> curProducts;       // 所有产品信息
    private List<Product> currentPageProducts; // 当前页产品信息
    private int currentPageIndex = 0;       // 当前页索引
    private final int PRODUCTS_PER_PAGE = 4; // 每页显示的产品数量

    private IProductService productService;

    public void onServiceConnected(IProductService service) {
        this.productService = service;
        curProducts = productService.getAllProducts();
        updateCurrentPage();
    }

    @Override
    public void onAttach(@NonNull android.content.Context context) {
        super.onAttach(context);
        fragmentContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_product_management, container, false);
        productListView = view.findViewById(R.id.product_list_view);
        previousPage = view.findViewById(R.id.previous_page);
        nextPage = view.findViewById(R.id.next_page);
        currentPage = view.findViewById(R.id.current_page);
        searchInput = view.findViewById(R.id.search_input);
        searchButton = view.findViewById(R.id.search_button);

        previousPage.setOnClickListener(v -> {
            if (currentPageIndex > 0) {
                currentPageIndex--;
                updateCurrentPage();
            }
        });

        nextPage.setOnClickListener(v -> {
            if ((currentPageIndex + 1) * PRODUCTS_PER_PAGE < curProducts.size()) {
                currentPageIndex++;
                updateCurrentPage();
            }
        });

        searchButton.setOnClickListener(v->setSearchButton());

        return view;
    }


    //查询按键
    private void setSearchButton(){
        String text = searchInput.getText().toString();
        curProducts = productService.searchProductsByNameOrCategory(text);
        currentPageIndex = 0;
        updateCurrentPage();
    }

    private void updateCurrentPage() {
        // 更新当前页面显示的产品
        int startIndex = currentPageIndex * PRODUCTS_PER_PAGE;
        int endIndex = Math.min(startIndex + PRODUCTS_PER_PAGE, curProducts.size());
        currentPageProducts = curProducts.subList(startIndex, endIndex);

        // 更新 ListView
        ProductAdapter adapter = new ProductAdapter(fragmentContext, currentPageProducts,productService);
        productListView.setAdapter(adapter);

        // 更新页面信息
        currentPage.setText(String.format("%d/%d", currentPageIndex + 1, getTotalPages()));

        // 更新按钮状态
        previousPage.setEnabled(currentPageIndex > 0);
        nextPage.setEnabled((currentPageIndex + 1) * PRODUCTS_PER_PAGE < curProducts.size());
    }

    private int getTotalPages() {
        return (int) Math.ceil((double) curProducts.size() / PRODUCTS_PER_PAGE);
    }

}
